package
{
	import flash.external.ExternalInterface;
	
	import models.LocalSetting;
	import models.vo.UserVO;
	
	import org.dexter.model.vo.VOBaseArray;
	import org.dexter.sendDexterEvent;
	
	import vo.GUserVO;

	public class UserListCtrl2
	{
		public var list:VOBaseArray = new VOBaseArray(GUserVO,"id",{publishPos:null,groupAddress:null});
		[DexterBinding]
		public var localSetting:GLocalSetting;
		public function UserListCtrl2()
		{
		}
		[DexterEvent]
		public function initUserList(userList:Object):void{
			list.setMap(userList);
			UserVO.self = getUserByID(UserVO.self.id);
			sendDexterEvent("callJs","initUserList",list);
		}
		[DexterEvent]
		public function $userOnline(u:Object):void{
			if(list.getItemByKey(u.id))return;
			sendDexterEvent("callJs","userOnline",u);
			var user:GUserVO = u is GUserVO?u as GUserVO:new GUserVO(u);
			list.setItem(user);
		}
		[DexterEvent]
		public function $userOffline(id:String):void{
			var user:GUserVO = getUserByID(id);
			if(!user)return;
			list.deleteItem(user);
			if(user.publishPos)sendDexterEvent("publisherChange",user,0);
			sendDexterEvent("callJs","userOffline",user);
		}
		[DexterEvent]
		public function $changeUserProperty(id:String,name:String,value:*):void{
			var user:GUserVO = getUserByID(id);
			if(!user)return;
			switch(name){
				case "publishPos":
					sendDexterEvent("publisherChange",user,value);
					break;
				case "name":
					if(user.isSelf)localSetting.userName = value;
					break;
				case "sex":
					if(user.isSelf)localSetting.userSex = value;
					break;
				case "applyAudio":
					sendDexterEvent("audioChange",user,value);
					break;
			}
			list.changeItemProperty(user,name,value);
			sendDexterEvent("callJs","changeUserProperty",user,name);
		}
		[DexterEvent][DexterExternal]
		public function getUserByID(id:String):GUserVO{
			return list.getItemByKey(id) as GUserVO;
		}
		[DexterEvent]
		public function getUserByGroupAddress(groupAddress:String):GUserVO{
			return list.getItemsByType("groupAddress",groupAddress).getRandomItem() as GUserVO;
		}
	}
}